package com.aisino.firstapp;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.concurrent.CompletableFuture;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("gct", "日志消息：onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");
        // 启用边缘到边缘显示效果
        EdgeToEdge.enable(this);
        // 设置Activity的布局文件
        setContentView(R.layout.activity_main);
        // 为视图设置窗口边衬监听器，处理系统状态栏和导航栏的边衬区域
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            // 获取系统状态栏和导航栏的边衬信息
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            // 设置视图的内边距，避免内容被系统UI遮挡
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        TextView viewById = findViewById(R.id.gct);
        CompletableFuture.runAsync(() -> {
            try {
                Log.e("gct", "日志消息：onCreate() 休眠5秒,5秒后注册点击事件");
                Thread.sleep(5000);
                Log.e("gct", "日志消息：onCreate() 休眠结束");
                runOnUiThread(() -> {
                    Log.i("gct", "日志消息：onCreate() runOnUiThread()");
                    viewById.setText("Hello World!");
                    for (int i = 0; i < 10; i++) {
                        Log.i("gct", "日志消息：onCreate() runOnUiThread() 循环第" + i + "次");
                    }
                });
                Log.e("gct", "日志消息：onCreate() runOnUiThread() 结束");
                // 此处注册 点击事件
                Button button = findViewById(R.id.button);
                // 设置按钮点击监听器，用于跳转到MainActivity2
                button.setOnClickListener(v -> {
                    Log.i("gct", "日志消息：onCreate() runOnUiThread() 开始了点击事件");
                    // 创建Intent对象，用于Activity之间的跳转
                    Intent intent = new Intent();
                    // 设置目标Activity为MainActivity2
                    intent.setClass(MainActivity.this, MainActivity2.class);
                    // 启动目标Activity
                    startActivity(intent);
                });
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        // 将id为gct的文字设置为中文的
        viewById.setText("你好世界！");
    }
}